<template>
  <div>
    <div class="sharingBox">
      <div class="sharingHeader">
        <span class="resourceTitle">资源模块</span>
        <el-select v-model="value" filterable placeholder="请选择" @change="selectLeiBie">
          <el-option
            v-for="(item,index) in options"
            :key="index"
            :label="item.title"
            :value="item.index"
          ></el-option>
        </el-select>
        <!-- <el-row :gutter="25">
          <el-col :span="6">
            <el-button @click="tabs = 0" :class="[tabs == 0 ? 'btnsSelect' : 'btns']" round>行业新规</el-button>
          </el-col>
          <el-col :span="6">
            <el-button @click="tabs = 1" :class="[tabs == 1 ? 'btnsSelect' : 'btns']" round>行业动态</el-button>
          </el-col>
          <el-col :span="6">
            <el-button @click="tabs = 2" :class="[tabs == 2 ? 'btnsSelect' : 'btns']" round>问卷调查</el-button>
          </el-col>
          <el-col :span="6">
            <el-button @click="tabs = 3" :class="[tabs == 3 ? 'btnsSelect' : 'btns']" round>电子书籍</el-button>
          </el-col>
        </el-row>-->
        <el-button class="addbtns" plain @click="toRouter">上传资源</el-button>
      </div>
      <div class="sharingMain">
        <div class="headNav">
          <!-- <div
          class="headNavText"
          :class="{ selectHeadNav: isSelect == '推荐' }"
          @click="selectHeadNav('推荐')"
          >推荐</div>-->
          <div
            class="headNavText"
            :class="{ selectHeadNav: isSelect == '热门' }"
            @click="selectHeadNav('热门')"
          >热门</div>
          <div
            class="headNavText"
            :class="{ selectHeadNav: isSelect == '最新' }"
            @click="selectHeadNav('最新')"
          >最新</div>
        </div>
        <el-divider class="titleDivider"></el-divider>
        <noMore v-if="listData.length == 0"></noMore>
        <div class="sharingList" v-for="(item, i) in listData" :key="i">
          <div class="TopBox">
            <div class="title">
              <div class="titleText" @click="toDetails(item)">{{ item.resourcename }}</div>
            </div>
            <div class="rightBox">
              上传时间：{{
              new Date(Date.parse(item.release_time)).toLocaleString()
              }}
            </div>
          </div>
          <div class="BottomBox">{{ item.textReplace }}</div>
          <div class="invitationBoxBottom">
            <div class="invitationBoxBottomItem">{{ item.reading }} 次浏览</div>
            <div class="invitationBoxBottomItem">{{ item.reply }} 回复</div>
            <div class="invitationBoxBottomItem">{{ item.praiseSize }} 赞</div>
          </div>
        </div>
        <div class="invitationPagination">
          <el-pagination
            background
            @current-change="handleCurrentChange"
            :page-size="limitSize"
            layout="prev, pager, next, jumper"
            :total="articleSize"
            :current-page.sync="currentPage"
          ></el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { get_resource } from "../../api/resorcefj";
import { get_resourcelabel } from "../../api/resourcelabel";
import noMore from "@/components/noMore";
export default {
  name: "resourceSharing",
  components: {
    noMore
  },
  data() {
    return {
      isSelect: "热门",
      list: [1, 2, 3, 4, 5, 6],
      listData: [],
      tabs: 0,
      skipSize: 0, //开始位置
      articleSize: 0, //总文章
      limitSize: 10,
      currentPage: 1,
      sortSelect: {
        sort: {
          reading: -1
        }
      }, //排序
      options: [],
      value: "全部",
      selectZiYuan: {}
    };
  },
  watch: {
    async $route(newRouter) {
      //监听路由是否变化
      if (newRouter.path == "/resourceSharing") {
        this.getResource(this.sortSelect);
      }
    }
  },
  async created() {
    let resourcelabelData = await get_resourcelabel();
    this.options = [{ title: "全部", index: "全部" }].concat(
      resourcelabelData.data
    );

    this.getResource(this.sortSelect);
  },
  methods: {
    async getResource(query) {
      console.log({
        query: {
          audit: true,
          ...this.selectZiYuan
        },
        ...query,
        skip: this.skipSize,
        limit: this.limitSize,
        count: true
      });

      let Data = await get_resource({
        query: {
          audit: true,
          ...this.selectZiYuan
        },
        ...query,
        skip: this.skipSize,
        limit: this.limitSize,
        count: true
      });
      console.log("listData资源", Data);
      this.listData = Data.data;
      this.articleSize = Data.countnum;
    },
    selectLeiBie() {
      if (this.value == "全部") {
        if (this.selectZiYuan.classify) {
          delete this.selectZiYuan.classify;
        } else {
          return;
        }
      } else {
        this.selectZiYuan.classify = this.value;
        this.skipSize = 0;
      }
      this.currentPage = 1;
      this.getResource(this.sortSelect);
    },
    selectHeadNav(nav) {
      if (this.isSelect == nav) return;
      this.isSelect = nav;
      this.skipSize = 0;
      this.currentPage = 1;
      if (this.isSelect == "热门") {
        this.sortSelect = {
          sort: {
            reading: -1
          }
        };
        this.getResource(this.sortSelect);
      } else {
        this.sortSelect = {
          sort: {
            release_time: -1
          }
        };
        this.getResource(this.sortSelect);
      }
    },
    handleCurrentChange(val) {
      this.skipSize = (val - 1) * this.limitSize;
      console.log(`当前页: ${val}`);
      this.getResource(this.sortSelect);
    },
    toRouter() {
      this.$router.push("/writeResources");
    },
    toDetails(item) {
      this.$router.push({
        path: "/resourceSharingDetails",
        query: {
          article_id: item._id
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.sharingBox {
  min-height: 800px;
  width: 100%;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  background-color: #ffffff;
  .sharingHeader {
    width: auto;
    height: 80px;
    line-height: 20px;
    display: flex;
    align-items: center;
    justify-content: flex-start;
    padding-left: 20px;
    padding-top: 10px;
    border-bottom: 1px solid #eaeaea;
    position: relative;
    .resourceTitle {
      font-size: 22px;
      color: #6c6c6c;
      font-weight: 600;
      margin-right: 20px;
    }
    .btns {
      border: none;
      display: block;
      color: #000000;
      background-color: #eaeaea;
      font-weight: 550;
      font-size: 16px;
      width: 120px;
      letter-spacing: 2px;
      width: 120px;
    }
    .btnsSelect {
      border: none;
      display: block;
      background-color: #eef5ff;
      color: #3257b5;
      font-weight: 550;
      font-size: 16px;
      width: 120px;
      letter-spacing: 2px;
      width: 120px;
    }
    .addbtns {
      display: block;
      color: #3257b5;
      background-color: #eaeaea;
      font-weight: 550;
      font-size: 16px;
      width: 120px;
      letter-spacing: 2px;
      width: 120px;
      position: absolute;
      right: 30px;
    }
  }
  .sharingMain {
    padding: 0 20px;
    width: 100%;
    overflow: auto;
    box-sizing: border-box;
    .titleDivider {
      margin: 10px 0 10px 0 !important;
    }
    .headNav {
      display: flex;
      color: #6c6c6c;
      margin-top: 10px;
      .headNavText {
        font-size: 22px;
        font-weight: 600;
        margin-right: 25px;
        cursor: pointer;
        &:hover {
          color: #1d2f70;
          font-weight: 600;
        }
      }
    }
    .sharingList {
      width: 1200px;
      // height: 150px;
      margin: 0 auto;
      border-bottom: 1px solid #eaeaea;
      display: flex;
      flex-direction: column;
      .TopBox {
        width: 100%;
        display: flex;
        align-items: center;
        flex-direction: row;
        justify-content: space-between;
        .title {
          height: 80px;
          width: 600px;
          font-size: 23px;
          font-weight: 600;
          color: #333;
          display: flex;
          align-items: center;
          .titleText {
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
            cursor: pointer;
            &:hover {
              color: #1d2f70;
              text-decoration: underline;
            }
          }
        }
        .rightBox {
          min-width: 100px;
          display: flex;
          align-items: center;
        }
      }
      .BottomBox {
        width: 100%;
        min-height: 70px;
        line-height: 35px;
        font-size: 18px;
        display: -webkit-box;
        -webkit-box-orient: vertical;
        -webkit-line-clamp: 2;
        overflow: hidden;
      }
      .invitationBoxBottom {
        display: flex;
        margin: 14px 0;
        color: #b3b2b2;
        justify-content: flex-end;
        .invitationBoxBottomItem {
          margin-right: 18px;
          font-size: 14px;
        }
      }
    }
    .invitationPagination {
      width: 80%;
      // margin: 0 auto;
      height: 80px;
      display: flex;
      align-items: center;
    }
  }
}
//选中样式
.selectHeadNav {
  color: #1d2f70;
}
</style>
